package registration;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Hashtable;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import swim.sessionbeans.UtilBeanRemote;

/**
 * Gestisce l'inserimento di un nuovo utente
 */

public class SearchServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private String step;

	public SearchServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();

		try {
			Context ctx = getInitialContext();
			UtilBeanRemote cerca = (UtilBeanRemote) ctx
					.lookup("UtilBeanRemote");

			step = request.getParameter("step");

			if (step.equals("1")) {

				List<String> macroareaList = cerca.getMacroareaList();
				request.setAttribute("macroareaList", macroareaList);
				RequestDispatcher dispatcher = request
						.getRequestDispatcher("/pagina-ricerca.jsp");
				dispatcher.forward(request, response);

			} else if (step.equals("2")) {
				String macroarea = request.getParameter("macroarea");
				List<String> abilitaList = cerca.getAbilitaList(macroarea);
				request.setAttribute("macroarea", macroarea);
				request.setAttribute("abilitaList", abilitaList);

				RequestDispatcher dispatcher = request
						.getRequestDispatcher("/pagina-ricerca2.jsp");
				dispatcher.forward(request, response);
			}

		} catch (Exception e) {
			e.printStackTrace();
			out = response.getWriter();
			out.print("<h1>Ricerca fallita!! - errore! </h1> <p> Esci dalla pagina e riprova </p>");
		}

	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();

		try {
			Context ctx = getInitialContext();
			UtilBeanRemote cerca = (UtilBeanRemote) ctx
					.lookup("UtilBeanRemote");

			step = (String) request.getParameter("step");

			if (step.equals("3")) {

				String path = System.getProperty("user.dir") + "/img/";

				String citta = (String) request.getParameter("citta");
				String abilita = (String) request.getParameter("abilita");
				List<String[]> risultatiList = cerca.ricerca("-1", abilita,
						citta);

				request.setAttribute("risultatiList", risultatiList);
				request.setAttribute("imgPath", path);

				RequestDispatcher dispatcher = request
						.getRequestDispatcher("/risultato-ricerca.jsp");
				dispatcher.forward(request, response);
			}

		} catch (Exception e) {
			e.printStackTrace();
			out = response.getWriter();
			out.print("<h1>Ricerca fallita!! - errore! </h1> <p> Esci dalla pagina e riprova </p>");
		}
	}

	/**
	 * Ottiene il Context utilizzato in seguito per ottenere i riferimenti ai
	 * RemoteSystems
	 * 
	 * @return Context utilizzato per la lookUp
	 * @throws Exception
	 *             Eccezione lanciata nel caso il metodo non riesca ad ottenere
	 *             il Context
	 */
	static public Context getInitialContext() throws Exception {

		Hashtable<String, String> env = new Hashtable<String, String>();

		env.put(Context.INITIAL_CONTEXT_FACTORY,
				"org.jnp.interfaces.NamingContextFactory");
		env.put(Context.PROVIDER_URL, "localhost:1099");

		return new InitialContext(env);

	}

}
